package com.google.devrel.example.android.protips.mvp;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.BatteryManager;
import android.view.View;

public class MonitorPresenter {

	private IMonitorView _view;

	public MonitorPresenter(IMonitorView view) {
		_view = view;
	}

	public void fillViewContent(View view, Context context) {

		// connectivity
		ConnectivityManager cm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNW = cm.getActiveNetworkInfo();
		if (activeNW != null) {
			_view.setConnectivityStatus(activeNW.isConnectedOrConnecting(), activeNW.getTypeName());
		}
		
		// battery
		IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
		Intent intent = context.registerReceiver(null, intentFilter);
		int batteryStat = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
		String stat = "";
		if (batteryStat==BatteryManager.BATTERY_STATUS_CHARGING) {
			stat = "BATTERY_STATUS_CHARGING";
		} else if (batteryStat==BatteryManager.BATTERY_STATUS_DISCHARGING) {
			stat = "BATTERY_STATUS_DISCHARGING";
		} else if (batteryStat==BatteryManager.BATTERY_STATUS_FULL) {
			stat = "BATTERY_STATUS_FULL";
		}
		_view.setBatteryStatus(stat);

	}
}
